﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename : D_ButtonExp.cs
//        description :
//
//        created by 周济 at 2010/8/22 15:59:11
//        mail:13912699233@139.com
//
//======================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using huaguwang.Common;
using huaguwang;

namespace huaguwang
{
    public class D_T_Button_Exp : D_T_Button
    {
        /// <summary>
        /// 获取分页数据
        /// </summary>
        public List<M_T_Button_Exp> GetListPage(int begin, int take, string MName, string Name, out int Count)
        {
            var tab = " dbo.T_Button AS a INNER JOIN dbo.T_Menu AS b ON a.MenuID = b.id ";
            var cod = " a.flag=1 and a.ButtonName like '%'+@Name+'%' and b.MenuName like '%'+@MName+'%'";

            var countsql = ExpandMSSQL.GetPageSQL(tab, cod);
            Count = MSSQLHelper.SQLConType.C1.Exp_GetSingle(countsql,
                new SqlParameter("@MName", SqlDbType.NVarChar, 30) { Value = MName },
                new SqlParameter("@Name", SqlDbType.NVarChar, 30) { Value = Name }).Exp_IntTryParse();

            var strSql = ExpandMSSQL.GetPageSQL(begin, take,
              " a.id, a.ButtonName, a.ButtonOrderBy, a.MenuID, a.Flag, b.MenuName ",
              tab,
              " a.id ",
              cod,
              " a.MenuID, a.ButtonOrderBy ");

            var list = huaguwang.MSSQLHelper.SQLConType.C1.Exp_GetList(strSql,
                    DR => new M_T_Button_Exp()
                    {
                        ButtonName = DR["ButtonName"].Exp_Trim(),
                        ButtonOrderBy = DR["ButtonOrderBy"].Exp_IntTryParse(),
                        Flag = true,
                        id = DR["id"].Exp_IntTryParse(),
                        MenuID = DR["MenuID"].Exp_IntTryParse(),
                        MenuName = DR["MenuName"].Exp_Trim(),

                    },
                    new SqlParameter("@MName", SqlDbType.NVarChar, 30) { Value = MName },
                    new SqlParameter("@Name", SqlDbType.NVarChar, 30) { Value = Name });

            return list;
        }

        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <returns></returns>
        public List<M_T_MenuExp> GetMenuList()
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT      a.id, a.MenuName, a.MenuURL, a.MenuParentID, a.MenuLevel, a.MenuParentList, a.MenuSonList, a.Flag, b.MenuName AS PName ");
            strSql.Append(" FROM         dbo.T_Menu AS a INNER JOIN ");
            strSql.Append(" dbo.T_Menu AS b ON a.MenuParentID = b.id ");
            strSql.Append(" WHERE     (a.Flag = 1) ");
            strSql.Append(" ORDER BY a.MenuParentList ");



            var list = huaguwang.MSSQLHelper.SQLConType.C1.Exp_GetList(strSql.ToString(),
               DR => new M_T_MenuExp()
               {
                   Flag = DR["Flag"].Exp_BoolTryParse(),
                   id = DR["id"].Exp_IntTryParse(),
                   MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                   MenuName = DR["MenuName"].Exp_Trim(),
                   MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                   MenuParentList = DR["MenuParentList"].Exp_Trim(),
                   MenuSonList = DR["MenuSonList"].Exp_Trim(),
                   MenuURL = DR["MenuURL"].Exp_Trim(),
                   PID = DR["MenuParentID"].Exp_IntTryParse(),
                   PName = DR["PName"].Exp_Trim(),
               }
               );
            return list;
        }

        public int UpdatePower(List<int> lid, int Gid)
        {
            var Refitem = MSSQLHelper.SQLConType.C1.ExecuteSqlTran((com, tran) =>
            {
                string strSql1 = " update t_power set flag=0 where powergroupid=@id ";
                com.Parameters.Add(new SqlParameter("@id", SqlDbType.Int) { Value = Gid });
                com.CommandText = strSql1;
                int intID = com.ExecuteNonQuery();
                com.Parameters.Clear();

                StringBuilder strSql = new StringBuilder();
                strSql.Append("insert into T_Power(");
                strSql.Append("PowerGroupID,ButtonID,Flag)");
                strSql.Append(" values (");
                strSql.Append("@PowerGroupID,@ButtonID,1)");
                SqlParameter[] parameters = {
                    new SqlParameter("@PowerGroupID", SqlDbType.Int,4),
                    new SqlParameter("@ButtonID", SqlDbType.Int,4),
                    new SqlParameter("@Flag", SqlDbType.Bit,1)};

                foreach (var item in lid)
                {
                    parameters[0].Value = Gid;
                    parameters[1].Value = item;
                    parameters[2].Value = true;
                    com.Parameters.Add(parameters[0]);
                    com.Parameters.Add(parameters[1]);
                    com.Parameters.Add(parameters[2]);
                    com.CommandText = strSql.ToString();
                    intID = 0;
                    intID = com.ExecuteNonQuery();
                    com.Parameters.Clear();
                    if (0 >= intID)
                    {
                        tran.Rollback();
                        return 0;
                    }
                }
                tran.Commit();
                return Gid;
            }            
            );
            return  Refitem;
        }

        public List<M_T_Button_Exp> GetListBut(int id, int uid)
        {
            string strSql = " SELECT     id, ButtonName, ButtonOrderBy, MenuID, Flag, IsLeft=cast(case when b.buttonid is null then 0 else 1 end  as bit)" +
                  " FROM         dbo.T_Button a " +
                  " left join  " +
                  " ( " +
                  " select buttonid from dbo.T_Power " +
                  " where powergroupid=@uid and flag=1 " +
                  " ) b " +
                  " on a.id=b.buttonid where a.menuid=@id and a.Flag = 1 ";

            var list = MSSQLHelper.SQLConType.C1.Exp_GetList(strSql,
                  DR => new M_T_Button_Exp()
                  {
                      ButtonName = DR["ButtonName"].Exp_Trim(),
                      ButtonOrderBy = DR["ButtonOrderBy"].Exp_IntTryParse(),
                      Flag = true,
                      id = DR["id"].Exp_IntTryParse(),
                      MenuID = DR["MenuID"].Exp_IntTryParse(),
                      IsLeft = DR["IsLeft"].Exp_BoolTryParse()
                  },
                  new SqlParameter("@id", SqlDbType.Int) { Value = id },
                  new SqlParameter("@uid", SqlDbType.Int) { Value = uid });
            return list;

        }

        public int GetPowerCount(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT  count(a.id) id ");
            strSql.Append(" FROM         dbo.T_Power AS a INNER JOIN ");
            strSql.Append(" dbo.T_PowerGroup AS b ON a.PowerGroupID = b.id ");
            strSql.Append(" WHERE     (a.ButtonID = @id) AND (a.Flag = 1) AND (b.Flag = 1) ");

            var intRef =huaguwang.MSSQLHelper.SQLConType.C1.Exp_GetSingle(strSql.ToString(),new SqlParameter("@id", SqlDbType.Int) { Value = id });

            return intRef.Exp_IntTryParse();
        }
    }
}
